package file

import (
	"strings"
)

/*
 From：   https://github.com/ouqiang/goutil
 Author：
 Date：   2023/09/27 12:30
*/

// ContainsString 是否包含字符串元素
func ContainsString(a []string, s string) bool {
	for _, item := range a {
		if item == s {
			return true
		}
	}

	return false
}

// ContainsInt 是否包含整型元素
func ContainsInt(a []int, i int) bool {
	for _, item := range a {
		if item == i {
			return true
		}
	}

	return false
}

// ContainsInt64 是否包含整型元素
func ContainsInt64(a []int64, i int64) bool {
	for _, item := range a {
		if item == i {
			return true
		}
	}

	return false
}

// RemoveEmptyString 移除空字符串
func RemoveEmptyString(a []string) []string {
	length := len(a)
	if length == 0 {
		return a
	}
	out := make([]string, 0, length)
	for _, item := range a {
		if item != "" {
			out = append(out, item)
		}
	}
	if len(out) == length {
		return a
	}

	return out
}

// Remove 移除元素
func Remove(a []string, s string) []string {
	length := len(a)
	if length == 0 {
		return a
	}
	out := make([]string, 0, length)
	for _, item := range a {
		if item != s {
			out = append(out, item)
		}
	}

	return out
}

// StringUnique 去重
func StringUnique(a []string) []string {
	num := len(a)

	if num <= 0 {
		return a
	}
	out := make([]string, 0, num)
	exists := make(map[string]bool, num)
	for _, item := range a {
		if exists[item] {
			continue
		}
		exists[item] = true
		out = append(out, item)
	}

	return out
}

// Int64Unique 去重
func Int64Unique(a []int64) []int64 {
	num := len(a)

	if num <= 0 {
		return a
	}
	out := make([]int64, 0, num)
	exists := make(map[int64]bool, num)
	for _, item := range a {
		if exists[item] {
			continue
		}
		exists[item] = true
		out = append(out, item)
	}

	return out
}

// ReplaceStrings 批量替换字符串
func ReplaceStrings(s string, old []string, replace []string) string {
	if s == "" {
		return s
	}
	if len(old) != len(replace) {
		return s
	}

	for i, v := range old {
		s = strings.Replace(s, v, replace[i], 1000)
	}

	return s
}

// InStringSlice 字符串是否在字符切片中
func InStringSlice(slice []string, element string) bool {
	element = strings.TrimSpace(element)
	for _, v := range slice {
		if strings.TrimSpace(v) == element {
			return true
		}
	}

	return false
}